package com.shanghaionstar.user.presenter;

import android.content.Context;
import android.os.Bundle;
import android.widget.EditText;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.more.response.PrepaidCardResponse;
import com.shanghaionstar.user.iview.IUsePrepayCardView;
import com.shanghaionstar.user.service.UsePrepayCardService;

import rx.functions.Action1;

/**
 * Created by Mengzhou Gu on 2016/8/3.
 */
public class UsePrepayCardPresenter extends BasePresenter<PrepaidCardResponse> {
    private Context context;
    private EditText et_carnumber_value;
    private EditText et_password_value;
    private Bundle bundle;

    private IUsePrepayCardView usePrepayCardView;
    private UsePrepayCardService service;

    public UsePrepayCardPresenter(IMVPScheduler mvpScheduler,Context context,EditText et_carnumber_value,EditText et_password_value,Bundle bundle) {
        super(mvpScheduler);
        this.context=context;
        this.et_carnumber_value=et_carnumber_value;
        this.et_password_value=et_password_value;
        this.bundle=bundle;
    }

    @Override
    public void setView(IMVPView<PrepaidCardResponse> view) {
        super.setView(view);
        usePrepayCardView = (IUsePrepayCardView) view;
        service = new UsePrepayCardService(context, et_carnumber_value, et_password_value, bundle);
        setServiceProvider(service);
    }

    public void getData(String subscriberId,String accountId){
        usePrepayCardView.setLoading(true);
        subscription=service.getObserval(subscriberId,accountId).subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<PrepaidCardResponse>() {
                    @Override
                    public void call(PrepaidCardResponse prepaidCardResponse) {
                        if(!CommonUtil.isNull(prepaidCardResponse)){
                            usePrepayCardView.setLoading(false);
                            usePrepayCardView.showSuccess(prepaidCardResponse);
                        }else {
                            usePrepayCardView.setLoading(false);
                            usePrepayCardView.showFailure();
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        usePrepayCardView.setLoading(false);
                        usePrepayCardView.showError(throwable);
                    }
                });
    }
}
